package pathways;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import pathways.Database.Implementation.CDatabase;
import pathways.SpeciesSpace.CInstantiation;
import pathways.SpeciesSpace.CSearchSpeciesSpace;
import pathways.SpeciesSpace.CSpeciesSpaceView;
import pathways.util.CUtil;

public class CEnzymeMouseListener implements IEnzymeMouseListener, ActionListener
{
  private CPathway pathway = null;
  private CReaction lastClickedRxn = null;
  
  public CEnzymeMouseListener(CPathway p)
  {
    pathway = p;
  }

  private JPopupMenu getPopupMenu(CReaction rxn)
  {
    JPopupMenu popup = new JPopupMenu();
    String label; 
    
    if (!CUtil.isMissingInfoReaction(rxn))
    {    
      if (rxn.getInstantiation() == null)
         label = "Instantiate";
      else
         label = "View Instantiation";
      
      JMenuItem item = new JMenuItem(label);
      JMenuItem item2 = new JMenuItem("View Reaction Details");
      
      item.addActionListener(this);
      item2.addActionListener(this);
      
      popup.add(item);
      popup.add(item2);
    }
    else
    {
      JMenuItem item = new JMenuItem("Missing info link");
      popup.add(item);  
    }
    
    return popup;
  }
  
  public void mouseClicked(CEnzymeMouseEvent e)
  {
    CReaction rxn = pathway.getReactionForEnzyme(e.getEnzyme());
    
    lastClickedRxn = rxn;
    
    JPopupMenu popup = getPopupMenu(rxn);
    popup.show(pathway,e.getMouseEvent().getX(),e.getMouseEvent().getY());
  }

  public void mousePressed(CEnzymeMouseEvent e)
  {

  }
  public void mouseReleased(CEnzymeMouseEvent e)
  {

  }

  public void mouseEntered(CEnzymeMouseEvent e)
  {

  }

  public void mouseExited(CEnzymeMouseEvent e)
  {

  }

  public void mouseDragged(CEnzymeMouseEvent e)
  {

  }

  public void mouseMoved(CEnzymeMouseEvent e)
  {

  }
  
  public void actionPerformed(ActionEvent e)
  {
    String command = e.getActionCommand();

    if (command.equals("Instantiate"))
    {
      doInstantiate(lastClickedRxn);
    }
    else if (command.equals("View Instantiation"))
    {
      doViewInstantiation(lastClickedRxn);
    }
    else if (command.equals("View Reaction Details"))
    {
      doShowReactionDetail(lastClickedRxn,pathway.getDatabase()); 
    }
  }
  
  private void doShowReactionDetail(CReaction rxn, CDatabase db)
  {
    CReactionDetailView rxnDetail = new CReactionDetailView(rxn,db);
    
    rxnDetail.show();  
  }
  
  private void doInstantiate(CReaction rxn)
  {
    CSearchSpeciesSpace frame = new CSearchSpeciesSpace(pathway.getOwner());
    
    CSpeciesSpaceView view = frame.getSpeciesSpace();
    
    view.setEditable(true);
    
    CInstantiation inst = new CInstantiation(pathway.getDatabase());
    
    inst.setReactionID((int)rxn.getID());
    
    view.load(inst,pathway.getDatabase());
    
    frame.show(); 
    
    if (view.isSaved())
       rxn.setInstantiation(inst);
    
  } 
  
  private void doViewInstantiation(CReaction rxn)
  {
    CSearchSpeciesSpace frame = new CSearchSpeciesSpace(pathway.getOwner());
    
    CSpeciesSpaceView view = frame.getSpeciesSpace();
    
    view.setEditable(true);
    
    CInstantiation inst = rxn.getInstantiation();
    
    view.load(inst,pathway.getDatabase());
    
    frame.show();
  } 
}